<template>
    <div style="height:100%;width:100%;position:relative;" ref="maindev">
        <div class="pagetop" style="height: 68px">
            <table style="width: 100%; font-size: 12px;" border="0">
                <tr>
                    <td style="width:60px">标本日期</td>
                    <td style="width:130px">
                        <el-date-picker v-model="sample_startdt" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width: 100%;min-width: 100px;" :clearable="false">
                        </el-date-picker>

                    </td>
                    <td align="center" style="width:30px">至</td>
                    <td style="width:130px">
                        <el-date-picker v-model="sample_enddt" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width: 100%;min-width: 100px;" :clearable="false">
                        </el-date-picker>
                    </td>
                    <td style="width:60px">报告单元</td>
                    <td style="width:160px">
                        <rm-ddl-rptunit v-model="sel_rptunit" width="100%" clearable @change="rptunitChange"></rm-ddl-rptunit>
                    </td>
                    <td style="width:60px">样本号</td>
                    <td style="width:180px">
                        <el-input size="small" style="width:100%;" placeholder="输入如1,2,5-8等格式" v-model="sel_sampleno"></el-input>
                    </td>
                    <td colspan="2">
                    </td>
                </tr>
                <tr>
                    <td colspan="13" align="right">
                        <el-button @click.native="BtnQuery" icon="el-icon-search" type="primary">查询</el-button>
                        <el-button @click.native="BtnExport" icon="el-icon-upload2">条码号导出</el-button>
                        <el-button @click.native="BtnRefresh" icon="el-icon-refresh">更新信息</el-button>
                        <el-button @click.native="BtnStop" icon="el-icon-refresh">停止</el-button>
                    </td>
                </tr>
            </table>
        </div>
        <div class="pagecenter" style="top: 74px">
            <vxe-table :data.sync="report_data" :loading="report_Loading" :select-config="{reserve: true}" :sort-config="{trigger: 'cell'}" border height="auto" highlight-current-row highlight-hover-column highlight-hover-row how-header-overflow ref="reportTable" resizable show-overflow size="mini" show-header-overflow :optimization="{animat: false}" :cell-class-name="reportCellName" header-align="center">
                <vxe-table-column field="rptunitname" sortable title="报告单元" width="130" show-overflow></vxe-table-column>
                <vxe-table-column align="center" field="sampledate" sortable title="标本日期" width="70" :formatter="['toDateString', 'yyyy-MM-dd']" show-overflow></vxe-table-column>
                <vxe-table-column field="sampleno" align="center" sortable title="标本号" width="60" show-overflow :sort-width="20"></vxe-table-column>
                <vxe-table-column field="cp_selection" title="选择" width="40px" align="center">
                    <template v-slot:header>
                        <el-checkbox @change="ChkAll($event)"></el-checkbox>
                    </template>
                    <template v-slot="{ row }">
                        <el-checkbox v-model="row.rmselectflag" true-label="1" false-label="0"></el-checkbox>
                    </template>
                </vxe-table-column>
                <vxe-table-column field="pat_name" align="center" sortable title="病人姓名" width="130" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_sexname" align="center" sortable title="性别" width="35" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_agestr" align="center" sortable title="年龄" width="40" show-overflow></vxe-table-column>
                <vxe-table-column field="barcode" sortable title="条码号" width="100" show-overflow></vxe-table-column>
                <vxe-table-column field="rmretmsg" sortable title="错误消息" width="160" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_no" sortable title="病人号" width="100" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_idcardno" align="center" sortable title="身份证/护照号" width="160" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_phone" sortable title="联系电话" width="100" show-overflow></vxe-table-column>

                <vxe-table-column field="specimen_codename" sortable title="标本类型" width="90" show-overflow></vxe-table-column>
                <vxe-table-column field="req_bedno" align="center" sortable title="床号" width="50" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_typecodename" align="center" sortable title="病人类别" width="80" show-overflow></vxe-table-column>
                <vxe-table-column field="req_dt" align="center" sortable title="申请时间" width="140" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_cardno" sortable title="就诊卡号" width="110" show-overflow></vxe-table-column>
                <vxe-table-column field="inp_id" sortable title="就诊号" width="100" show-overflow></vxe-table-column>
                <vxe-table-column field="sampled_dt" align="center" sortable title="采样时间" width="140" show-overflow></vxe-table-column>
                <vxe-table-column field="recieve_dt" align="center" sortable title="接收时间" width="140" show-overflow></vxe-table-column>
                <vxe-table-column field="input_dt" align="center" sortable title="录入时间" width="140" show-overflow></vxe-table-column>
                <vxe-table-column field="report_dt" align="center" sortable title="报告时间" width="140" show-overflow></vxe-table-column>
                <vxe-table-column field="report_username" align="center" sortable title="检验医生" width="70" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_id" sortable title="病人ID" width="130" show-overflow></vxe-table-column>
                <vxe-table-column field="reportid" sortable title="报告ID" width="160" show-overflow></vxe-table-column>
                <vxe-table-column field="hospcodename" sortable title="送检医院" width="180" show-overflow></vxe-table-column>
                <vxe-table-column field="pat_address" sortable title="病人地址" width="200" show-overflow></vxe-table-column>
            </vxe-table>
        </div>
    </div>
</template>
<script>
import XEUtils from "xe-utils"; //引入工具类
export default {
    name: "LIS492",
    data() {
        return {
            report_Loading: false,
            report_data: [],
            sel_rptunit: "",
            sample_startdt: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
            sample_enddt: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
            sel_sampleno: "",
            stopflag: false,
            rptunitname: "",
        };
    },
    created() {},
    watch: {},
    mounted() {},
    updated() {},
    methods: {
        ChkAll(e) {
            if (this.report_data != null && this.report_data.length > 0) {
                this.report_data.forEach(item => {
                    //利用lmp字段来存储选择标志
                    item.rmselectflag = e == true ? "1" : "0";
                });
            }
        },
        BtnQuery() {
            this.report_Loading = true;
            this.$tool
                .request({
                    url: "LIS/LIS4/LIS492",
                    method: "post",
                    timeout: 600000,
                    data: {
                        action: "query",
                        sample_startdt: this.sample_startdt,
                        sample_enddt: this.sample_enddt,
                        sel_rptunit: this.sel_rptunit,
                        sel_sampleno: this.sel_sampleno
                    }
                })
                .then(res => {
                    if (res.flag == -1) {
                        this.$message({
                            type: "error",
                            message: res.msg
                        });
                        this.report_Loading = false;
                        return false;
                    }
                    this.report_data = res.data;
                    this.report_Loading = false;
                })
                .catch(e => {
                    this.$message.error(e);
                });
        },
        rptunitChange(code, name) {
            this.sel_rptunit = code;
            this.rptunitname = name;
        },
        BtnExport() {
            let exportColumn = [];
            exportColumn.push({
                title: "条码号",
                field: "barcode"
            });
            let filtertxt = this.getExportFilter();
            this.$pub.exportTableExcel(this.report_data, exportColumn, "虚条码导出.xlsx", filtertxt);
        },
        // 获取当前导出文件的所有查询条件
        getExportFilter() {
            let filtertxt = "标本日期:" + XEUtils.toDateString(this.sample_startdt, "yyyy-MM-dd") + "至" + XEUtils.toDateString(this.sample_enddt, "yyyy-MM-dd") + "; ";
            if(this.sel_rptunit) filtertxt += "报告单元:" + this.rptunitname + "; ";
            if(this.sel_sampleno) filtertxt += "样本号:" + this.sel_sampleno + "; ";
            return filtertxt;
        },
        BtnRefresh() {
            this.stopflag = false;
            var selrows = this.report_data.filter(t => t.rmselectflag == "1");
            if (selrows != null && selrows.length > 0) {
                this.ExecBtnRefresh(selrows, 0);
            }
        },
        ExecBtnRefresh(selrows, index) {
            if (this.stopflag) {
                return;
            }
            if (index >= selrows.length) {
                return;
            } else {
                var row = selrows[index];
                this.$tool
                    .request({
                        url: "LIS/LIS4/LIS492",
                        method: "post",
                        data: {
                            action: "refresh",
                            barcode: row.barcode,
                            reportid: row.reportid
                        }
                    })
                    .then(res => {
                        if (res.flag == -1) {
                            row.rmretmsg = res.msg;
                        } else {
                            row.rptunitname = res.report.rptunitname;
                            row.sampledate = res.report.sampledate;
                            row.sampleno = res.report.sampleno;
                            row.pat_name = res.report.pat_name;
                            row.pat_no = res.report.pat_no;
                            row.pat_sexname = res.report.pat_sexname;
                            row.pat_agestr = res.report.pat_agestr;
                            row.barcode = res.report.barcode;
                            row.pat_idcardno = res.report.pat_idcardno;
                            row.pat_phone = res.report.pat_phone;
                            row.rmretmsg = res.report.rmretmsg;
                            row.specimen_codename = res.report.specimen_codename;
                            row.req_bedno = res.report.req_bedno;
                            row.pat_typecodename = res.report.pat_typecodename;
                            row.req_dt = res.report.req_dt;
                            row.pat_cardno = res.report.pat_cardno;
                            row.inp_id = res.report.inp_id;
                            row.sampled_dt = res.report.sampled_dt;
                            row.recieve_dt = res.report.recieve_dt;
                            row.input_dt = res.report.input_dt;
                            row.report_dt = res.report.report_dt;
                            row.report_username = res.report.report_username;
                            row.pat_id = res.report.pat_id;
                            row.reportid = res.report.reportid;
                            row.hospcodename = res.report.hospcodename;
                            row.pat_address = res.report.pat_address;
                        }
                        this.ExecBtnRefresh(selrows, ++index);
                    })
                    .catch(e => {
                        this.ExecBtnRefresh(selrows, ++index);
                    });
            }
        },
        BtnStop() {
            this.stopflag = true;
        },
        reportCellName({ row, rowIndex, column, columnIndex }) {
            if (column.property == "rmretmsg") {
                if (!!row.rmretmsg && row.rmretmsg != "更新成功") {
                    return "col-red";
                }
            }
        }
    }
};
</script>

<style scoped>
.middle-tab-bottom {
    position: absolute;
    top: 0px;
    width: 100%;
    bottom: 0px;
    overflow: auto;
}
.pagetop {
    left: 0;
    right: 0;
    height: 305px;
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    border: solid 1px #ccc;
    position: absolute;
    z-index: 99;
    overflow-x: auto;
    white-space: nowrap;
}
.pagecenter {
    position: absolute;
    top: 125px;
    width: 100%;
    left: 0;
    bottom: 0px;
    overflow: auto;
}
.pagebottom {
    position: absolute;
    width: 100%;
    left: 0;
    margin-top: 4px;
    height: 36px;
    bottom: 0px;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    border: solid 1px #ccc;
}

.right-inser {
    position: absolute;
    top: 0;
    bottom: 0px;
    overflow: auto;
    width: 100%;
    right: 0px;
}

table tr td:nth-child(odd) {
    text-align: right;
    white-space: nowrap;
}
table tr td:nth-child(even) {
    text-align: left;
}
table tr td:nth-child(1) {
    width: 80px;
}
table {
    font-size: 12px;
    border-collapse: separate;
    border-spacing: 5px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader-icon1 {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 50px;
    line-height: 50px;
    text-align: center;
}
.avatar1 {
    width: 178px;
    height: 50px;
    display: block;
}
.middle-tab-top {
    position: absolute;
    top: 0px;
    width: 100%;
    height: 200px;
}
.middle-bottom {
    position: absolute;
    top: 37px;
    width: 100%;
    bottom: 0px;
    overflow: auto;
}
.middle-tab-bottom {
    position: absolute;
    top: 203px;
    width: 100%;
    bottom: 0px;
    overflow: auto;
}
.poppagetop {
    width: 100%;
    height: 24px;
    -webkit-box-pack: left;
    -webkit-box-align: center;
    position: absolute;
    /* border: 1px solid #ccc; */
    z-index: 999;
}
.poppagecenter {
    position: absolute;
    overflow: auto;
    top: 27px;
    width: 100%;
    height: 450px;
    left: 0;
}
::v-deep .vxe-table .vxe-body--column.col-red {
    color: red;
}
</style>